package com.gidgabs.modules.api.service.impl;

import com.gidgabs.common.utils.Assist;
import com.gidgabs.common.utils.LangUtil;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.dto.TbProductCommentDto;
import com.gidgabs.dto.TbUserDto;
import com.gidgabs.modules.api.service.TbProductCommentLikeService;
import com.gidgabs.modules.api.service.TbProductCommentService;
import com.gidgabs.modules.dao.TbProductCommentDao;
import com.gidgabs.modules.dao.TbProductCommentLikeDao;
import com.gidgabs.modules.entity.TbProductCommentEntity;
import com.gidgabs.modules.entity.TbProductCommentLikeEntity;
import com.gidgabs.modules.entity.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class TbProductCommentServiceImpl implements TbProductCommentService {
	@Autowired
	private TbProductCommentDao productCommentDao;

    @Autowired
    private TbProductCommentLikeDao productCommentLikeDao;
	
	@Override
	public TbProductCommentEntity queryObject(Integer id){
		return productCommentDao.queryObject(id);
	}
	
	@Override
	public List<TbProductCommentEntity> queryList(Map<String, Object> map){
		return productCommentDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return productCommentDao.queryTotal(map);
	}
	
	@Override
	public void save(TbProductCommentEntity productComment){
		productCommentDao.save(productComment);
	}
	
	@Override
	public void update(TbProductCommentEntity productComment){
		productCommentDao.update(productComment);
	}
	
	@Override
	public void delete(Integer id){
		productCommentDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		productCommentDao.deleteBatch(ids);
	}

	@Override
	public int queryCount(TbProductCommentEntity tbProductCommentEntity) {
		return productCommentDao.queryCount(tbProductCommentEntity);
	}

	@Override
	public ReturnJson<TbProductCommentDto> queryListBy(Integer pageSize, Integer pageNo, Integer productId, TbUser tbUser) {
		Assist assist = new Assist();
		Assist.WhereOrder createTime = Assist.order("create_time", false);
		assist.andEq("comment_id_slave",productId);
		assist.setOrder(createTime);
		assist.setRowSize(pageSize);
		assist.setStartRow(LangUtil.firstLimit(pageSize, pageNo));
		List<TbProductCommentEntity> tbProductCommentEntityList =  productCommentDao.selectTbProductComment(assist);
		long count = productCommentDao.getTbProductCommentRowCount(assist);
		//转成dto 并判断是否被当前用户点赞
		List<TbProductCommentDto>  tbProductCommentDtoList = new ArrayList<>();
		for (TbProductCommentEntity tbProductCommentEntity: tbProductCommentEntityList) {
            TbProductCommentDto tbProductCommentDto = tbProductCommentEntity.toDto();
            //判断点赞 不为空 设置为true
            TbProductCommentLikeEntity tbProductCommentLikeEntity = new TbProductCommentLikeEntity();
            tbProductCommentLikeEntity.setUserId(tbUser.getUserId());
            tbProductCommentLikeEntity.setProductCommentId(tbProductCommentDto.getId());
            if(productCommentLikeDao.queryByObject(tbProductCommentLikeEntity) != null){
                //设置为点赞 给前端显示
                tbProductCommentDto.setIfLike(true);
            }
			tbProductCommentDtoList.add(tbProductCommentDto);
		}
		return ReturnJson.success("查询成功", tbProductCommentDtoList, count, pageSize, pageNo);
	}

}
